<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>App版本管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            background-color: #f8f9fa;
        }
        .success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .version-info {
            margin-top: 20px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
    </style>
</head>
<body>
    <h1>App版本管理</h1>
    
    <h2>上传新版本</h2>
    <form id="uploadForm" enctype="multipart/form-data">
        <div class="form-group">
            <label for="versionCode">版本号:</label>
            <input type="number" id="versionCode" name="versionCode" required>
        </div>
        
        <div class="form-group">
            <label for="versionName">版本名称:</label>
            <input type="text" id="versionName" name="versionName" required>
        </div>
        
        <div class="form-group">
            <label for="description">版本描述:</label>
            <textarea id="description" name="description" rows="4"></textarea>
        </div>
        
        <div class="form-group">
            <label for="file">APK文件:</label>
            <input type="file" id="file" name="file" accept=".apk" required>
        </div>
        
        <button type="submit">上传版本</button>
    </form>
    
    <div id="uploadResult" class="result" style="display: none;"></div>
    
    <h2>获取最新版本信息</h2>
    <button id="getVersionBtn">获取最新版本</button>
    
    <div id="versionInfo" class="version-info" style="display: none;"></div>
    
    <h2>下载最新版本</h2>
    <a href="/api/app/download/latest" class="button" id="downloadLink" style="display: none;">
        <button type="button">下载最新版本</button>
    </a>

    <script>
        document.getElementById('uploadForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = new FormData();
            formData.append('versionCode', document.getElementById('versionCode').value);
            formData.append('versionName', document.getElementById('versionName').value);
            formData.append('description', document.getElementById('description').value);
            formData.append('file', document.getElementById('file').files[0]);
            
            fetch('/api/app/upload', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                const resultDiv = document.getElementById('uploadResult');
                resultDiv.style.display = 'block';
                
                if (data.code === 200) {
                    resultDiv.className = 'result success';
                    resultDiv.innerHTML = '<h3>上传成功</h3>' +
                        '<p>版本号: ' + data.data.versionCode + '</p>' +
                        '<p>版本名称: ' + data.data.versionName + '</p>' +
                        '<p>文件大小: ' + (data.data.fileSize / 1024 / 1024).toFixed(2) + ' MB</p>';
                } else {
                    resultDiv.className = 'result error';
                    resultDiv.innerHTML = '<h3>上传失败</h3>' +
                        '<p>' + data.msg + '</p>';
                }
            })
            .catch(error => {
                const resultDiv = document.getElementById('uploadResult');
                resultDiv.style.display = 'block';
                resultDiv.className = 'result error';
                resultDiv.innerHTML = '<h3>上传失败</h3>' +
                    '<p>' + error.message + '</p>';
            });
        });
        
        document.getElementById('getVersionBtn').addEventListener('click', function() {
            fetch('/api/app/version')
            .then(response => response.json())
            .then(data => {
                const versionInfoDiv = document.getElementById('versionInfo');
                versionInfoDiv.style.display = 'block';
                document.getElementById('downloadLink').style.display = 'inline-block';
                
                if (data.code === 200) {
                    versionInfoDiv.innerHTML = '<h3>最新版本信息</h3>' +
                        '<p><strong>版本号:</strong> ' + data.data.versionCode + '</p>' +
                        '<p><strong>版本名称:</strong> ' + data.data.versionName + '</p>' +
                        '<p><strong>描述:</strong> ' + (data.data.description || '无') + '</p>' +
                        '<p><strong>文件大小:</strong> ' + (data.data.fileSize / 1024 / 1024).toFixed(2) + ' MB</p>' +
                        '<p><strong>上传时间:</strong> ' + new Date(data.data.createDate).toLocaleString() + '</p>';
                } else {
                    versionInfoDiv.innerHTML = '<h3>获取失败</h3>' +
                        '<p>' + data.msg + '</p>';
                }
            })
            .catch(error => {
                const versionInfoDiv = document.getElementById('versionInfo');
                versionInfoDiv.style.display = 'block';
                versionInfoDiv.innerHTML = '<h3>获取失败</h3>' +
                    '<p>' + error.message + '</p>';
            });
        });
    </script>
</body>
</html>